package com.net.TCP04.chat;

import java.io.*;
import java.net.Socket;

/**
 * 创建客户端：发送数据+接收数据
 *写出数据：输出流
 * 读取数据：输入流
 *
 *
 * 输入流和输出流在同一个线程内  必须独立处理，彼此独立
 * @author jiangbang
 * @date 2016/7/26
 */
public class Client {
    public static void main(String[] args) throws IOException {
        //1、创建客户端，连接上服务器
        Socket client = new Socket("localhost",9999);
        //控制台输入流
        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        DataInputStream dis = new DataInputStream(client.getInputStream());
        while(true){
            String info = console.readLine();
            //输出流
            dos.writeUTF(info);
            dos.flush();
            //输入流
            String msg = dis.readUTF();
            System.out.println(msg);
        }

    }

    //原始版客户端
    public void demoClient() throws IOException {
        //1、创建客户端，连接上服务器
        Socket client = new Socket("localhost",9999);
//        //控制台输入流
//        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
//        String info = console.readLine();

        //输出流
        DataOutputStream dos = new DataOutputStream(client.getOutputStream());
        dos.writeUTF("数据");
        dos.flush();
        //输入流
        DataInputStream dis = new DataInputStream(client.getInputStream());
        String msg = dis.readUTF();
        System.out.println(msg);
    }
}
